import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: {},
    curPage: ''
  },
  mutations: {
    changeCurPage (state, path) {
      state.curPage = path
    },
    changeUser (state, user) {
      state.user = user
      if (user) {
        window.localStorage.setItem('access_token', state.user.access_token)
        window.localStorage.setItem('appid', state.user.appid)
        window.localStorage.setItem('secret', state.user.secret)
        window.localStorage.setItem('grant_type', state.user.grant_type)
        window.localStorage.setItem('token', user.token)
      } else {
        state.user = {}
        window.localStorage.setItem('access_token', '')
        window.localStorage.setItem('appid', '')
        window.localStorage.setItem('secret', '')
        window.localStorage.setItem('grant_type', '')
        window.localStorage.setItem('token', user.token)
      }
    },
    changeToken (state, token) {
      state.user.access_token = token
      window.localStorage.setItem('access_token', token)
    }
  },
  actions: {
  },
  modules: {
  }
})
